﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TMS
{
    public partial class CreateEditProductControl : UserControl
    {
        public MySqlDataAdapter MyDA = new MySqlDataAdapter();
        public Users ActiveUser = Program.GetActiveUser;
        bool edit = false;
        int ID = 0;

        public CreateEditProductControl()
        {
            Program.GetMainForm.Text = "TMS - Нов производ";
            InitializeComponent();
            fillCategory1CB();
        }

        public CreateEditProductControl(DataGridViewRow parameters)
        {
            //THIS IS THE EDIT VIEW
            Program.GetMainForm.Text = "TMS - Продукт: " + parameters.Cells[10].Value.ToString();
            InitializeComponent();
            edit = true;
            

            fillCategory1CB();

            cbbCategory1.SelectedValue =  parameters.Cells[1].Value.ToString();            
            cbbCategory2.SelectedValue = parameters.Cells[2].Value.ToString();          
            cbbCategory3.SelectedValue =  parameters.Cells[3].Value.ToString();
            cmbMeasure.SelectedItem = parameters.Cells[11].Value.ToString();           
            txtName.Text = parameters.Cells[4].Value.ToString();
            txtPartNum.Text = parameters.Cells[5].Value.ToString();
            txtNominalPrice.Text = parameters.Cells[6].Value.ToString();

            if (Convert.ToBoolean(parameters.Cells[7].Value))
                rbService.Checked = true;
            else
                rbProduct.Checked = true;

            cbTrack.Checked = Convert.ToBoolean(parameters.Cells[8].Value);

            txtMinQuantity.Text = parameters.Cells[9].Value.ToString();
            txtDescription.Text = parameters.Cells[10].Value.ToString();
            
            ID = Convert.ToInt32(parameters.Cells[0].Value);
        }

        private void fillCategory1CB()
        {
            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection mysqlCon = new MySqlConnection(connString);
            mysqlCon.Open();

            string sqlSelectAll = "SELECT * FROM category_1";
            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

            DataTable table = new DataTable();
            MyDA.Fill(table);

            DataRow row = table.NewRow();
            row["name"] = "<--Изберете сектор-->";
            table.Rows.InsertAt(row, 0);

            cbbCategory1.DataSource = table;
            cbbCategory1.ValueMember = "id";
            cbbCategory1.DisplayMember = "name";

            mysqlCon.Close();
        }

        private void cbbCategory1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cmb = (ComboBox)sender;

            if ((cmb.SelectedIndex == 0)&&(!edit))
            {

                lblCategory2.Hide();
                cbbCategory2.Hide();

                lblCategory3.Hide();
                cbbCategory3.Hide();

                lblName.Hide();
                txtName.Hide();

                lblPartNum.Hide();
                txtPartNum.Hide();

                lblNominalPrice.Hide();
                txtNominalPrice.Hide();

                lblMinQuantity.Hide();
                txtMinQuantity.Hide();

                lblDesc.Hide();
                txtDescription.Hide();

                cbTrack.Hide();

            }
            else
            {

                string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

                MySqlConnection mysqlCon = new MySqlConnection(connString);
                mysqlCon.Open();

                string selectedCat1 = cmb.SelectedValue.ToString();

                string sqlSelectAll = "SELECT * FROM category_2 WHERE cat1_id = '" + selectedCat1 + "'";
                MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

                DataTable table = new DataTable();
                MyDA.Fill(table);
                
                DataRow row = table.NewRow();
                row["name"] = "<--Изберете отсек-->";
                table.Rows.InsertAt(row, 0);

                cbbCategory2.DataSource = table;
                cbbCategory2.ValueMember = "id";
                cbbCategory2.DisplayMember = "name";

                lblCategory2.Show();
                cbbCategory2.Show();

                mysqlCon.Close();
            }
        }

        private void cbbCategory2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cmb = (ComboBox)sender;

            if ((cmb.SelectedIndex == 0)&&(!edit))
            {
                lblCategory3.Hide();
                cbbCategory3.Hide();

                lblName.Hide();
                txtName.Hide();

                lblPartNum.Hide();
                txtPartNum.Hide();

                lblNominalPrice.Hide();
                txtNominalPrice.Hide();

                lblMinQuantity.Hide();
                txtMinQuantity.Hide();

                lblDesc.Hide();
                txtDescription.Hide();

                cbTrack.Hide();

            }
            else
            {

                string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

                MySqlConnection mysqlCon = new MySqlConnection(connString);
                mysqlCon.Open();

                string selectedCat1 = cbbCategory1.SelectedValue.ToString();
                string selectedCat2 = cmb.SelectedValue.ToString();

                string sqlSelectAll = "SELECT * " +
                                      "FROM category_3 " +
                                      "WHERE cat1_id = '" + selectedCat1 + "' " +
                                        "AND cat2_id = '" + selectedCat2 + "'";
                MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

                DataTable table = new DataTable();
                MyDA.Fill(table);

                DataRow row = table.NewRow();
                row["name"] = "<--Изберете тип-->";
                table.Rows.InsertAt(row, 0);

                cbbCategory3.DataSource = table;
                cbbCategory3.ValueMember = "id";
                cbbCategory3.DisplayMember = "name";

                lblCategory3.Show();
                cbbCategory3.Show();

                mysqlCon.Close();
            }
        }

        private void cbbCategory3_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cmb = (ComboBox)sender;

            if ((cmb.SelectedIndex == 0)&&(!edit))
            {
                lblName.Hide();
                txtName.Hide();

                lblPartNum.Hide();
                txtPartNum.Hide();

                lblNominalPrice.Hide();
                txtNominalPrice.Hide();

            }
            else
            {
                lblName.Show();
                txtName.Show();

                lblPartNum.Show();
                txtPartNum.Show();

                lblNominalPrice.Show();
                txtNominalPrice.Show();

                lblMinQuantity.Show();
                txtMinQuantity.Show();

                lblDesc.Show();
                txtDescription.Show();

                cbTrack.Show();


                lbMeasure.Visible = true;
                cmbMeasure.Visible = true;
            }
        }

      

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }

        private void ToStDelete_Click(object sender, EventArgs e)
        {
            if (ActiveUser.GetName() == "Администратор")
            {
                if (MessageBox.Show("Сигурни сте дека сакате да го избришете продуктот?", "Потврда за бришење", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Product prod = new Product(ID);
                    prod.DeleteProductFromDB();
                    DataGridViewRow parametars = new DataGridViewRow();
                    DataGridView dgvtmp = new DataGridView();
                    dgvtmp.ColumnCount = 3;
                    dgvtmp.RowCount = 1;
                    dgvtmp.Rows[0].Cells[0].Value = cbbCategory1.SelectedValue;
                    dgvtmp.Rows[0].Cells[1].Value = cbbCategory2.SelectedValue;
                    dgvtmp.Rows[0].Cells[2].Value = cbbCategory3.SelectedValue;

                    parametars = dgvtmp.Rows[0];

                    Program.GetMainForm.ShowControl(ControlsEnum.PRODUCTS_CONTROL, parametars);
                    this.Dispose();
                }

                }
                else
                    MessageBox.Show("Само администраторот на TMS може да брише продукти");
        }

        private void ToStSave_Click(object sender, EventArgs e)
        {
            int type = rbProduct.Checked ? 0 : 1;
            int track = cbTrack.Checked ? 1 : 0;
            Product prod = new Product(ID, Convert.ToInt32(cbbCategory1.SelectedValue), Convert.ToInt32(cbbCategory2.SelectedValue), Convert.ToInt32(cbbCategory3.SelectedValue), txtName.Text, txtPartNum.Text, Convert.ToDouble(txtNominalPrice.Text), type, track, Convert.ToInt32(txtMinQuantity.Text), txtDescription.Text, cmbMeasure.SelectedItem.ToString());

            if (edit)
            {
                prod.UpdateProductInDB();
            }
            else
            {
                if (prod.CheckExistingProductInDB())
                {
                    MessageBox.Show("Веќе постои продукт со исто име или Про.Бр.!");
                }
                else
                    prod.InsertProductInDB();
            }

            this.Hide();

            DataGridViewRow parametars = new DataGridViewRow();
            DataGridView dgvtmp = new DataGridView();
            dgvtmp.ColumnCount = 3;
            dgvtmp.RowCount = 1;
            dgvtmp.Rows[0].Cells[0].Value = cbbCategory1.SelectedValue;
            dgvtmp.Rows[0].Cells[1].Value = cbbCategory2.SelectedValue;
            dgvtmp.Rows[0].Cells[2].Value = cbbCategory3.SelectedValue;

            parametars = dgvtmp.Rows[0];

            Program.GetMainForm.ShowControl(ControlsEnum.PRODUCTS_CONTROL, parametars);
        }

        private void ToStSaveAsNew_Click(object sender, EventArgs e)
        {
            int type = rbProduct.Checked ? 0 : 1;
            int track = cbTrack.Checked ? 1 : 0;
            Product prod;

            prod = new Product(ID, Convert.ToInt32(cbbCategory1.SelectedValue), Convert.ToInt32(cbbCategory2.SelectedValue), Convert.ToInt32(cbbCategory3.SelectedValue), txtName.Text, txtPartNum.Text, Convert.ToDouble(txtNominalPrice.Text), type, track, Convert.ToInt32(txtMinQuantity.Text), txtDescription.Text, cmbMeasure.SelectedItem.ToString());

            if (prod.CheckExistingProductInDB())
            {
                MessageBox.Show("Веќе постои продукт со исто име или Про.Бр.!");
            }
            else
            {
                prod.InsertProductInDB();

                MessageBox.Show("Продуктот е успешно внесен.");

                this.Hide();

                DataGridViewRow parametars = new DataGridViewRow();
                DataGridView dgvtmp = new DataGridView();
                dgvtmp.ColumnCount = 3;
                dgvtmp.RowCount = 1;
                dgvtmp.Rows[0].Cells[0].Value = cbbCategory1.SelectedValue;
                dgvtmp.Rows[0].Cells[1].Value = cbbCategory2.SelectedValue;
                dgvtmp.Rows[0].Cells[2].Value = cbbCategory3.SelectedValue;

                parametars = dgvtmp.Rows[0];

                Program.GetMainForm.ShowControl(ControlsEnum.PRODUCTS_CONTROL, parametars);
            }
        }

        

       

    }
}
